package com.android.sdlc.checkterminal;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity extends AppCompatActivity {

    private Button btn_pyString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPython();
        initView();
        initListener();
    }

    // 初始化Python环境
    void initPython(){
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
    }

    private void initView(){
        btn_pyString = findViewById(R.id.btn_pyString);
    }

    private void initListener(){
        btn_pyString.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callPythonCode();
            }
        });
    }

    // 调用python代码
    void callPythonCode(){
        Python py = Python.getInstance();
        // testpy.py模块中的greet函数，并传一个参数
        // 等价用法：py.getModule("testpy").get("greet").call("Android");
        py.getModule("testpy").callAttr("greet", "Android");

        PyObject numa = py.getModule("testpy").callAttr("arrayNum");
        Log.e("tag","numa="+numa);
    }
}
